package com.etoak.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;

/**
 * @program: activemq-SpringMVC
 * @description: Spring Root 配置
 * @author: 管浩杰
 * @create: 2021-03-17 09:34
 **/



@Configuration
@ComponentScan(basePackages = {"com.etoak"},
        excludeFilters = {
                @ComponentScan.Filter(type= FilterType.ANNOTATION,
                        classes = { Controller.class,
                                RestController.class,
                                RestController.class,
                                ControllerAdvice.class,
                                EnableWebMvc.class})},
        includeFilters = {
                @ComponentScan.Filter(type = FilterType.ANNOTATION,
                        classes = {
                                Service.class,
                                Repository.class
                        }
                )
        }

)
@PropertySource(value = "classpath:db.properties") // 引入配置文件

@MapperScan("com.etoak.mapper")
@EnableTransactionManagement // <tx:annotation-driven>
@Import(ActiveMQConfig.class)
public class RootConfig {
    @Value("${db.driver}")
    private String driverClassName;

    @Value("${db.url}")
    private String url;

    @Value("${db.username}")
    private String username;

    @Value("${db.password}")
    private String password;


    /* 数据源 */
    @Bean
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setInitialSize(10);
        dataSource.setMaxActive(30);
        return dataSource;
    }

    /* sqlSessionFactoryBean 配置sqlsessionfactoryBean 给 */
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(){
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(this.dataSource());
        factoryBean.setTypeAliasesPackage("com.etoak.bean");

        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        try{
            Resource[] resources = resolver.getResources("classpath:mapper/*.xml");
            factoryBean.setMapperLocations(resources);
        }catch(IOException e){
            e.printStackTrace();
        }

        PageInterceptor pageInterceptor = new PageInterceptor();

        Properties properties = new Properties();
        properties.setProperty("reasonable", "true");

        pageInterceptor.setProperties(properties);
        factoryBean.setPlugins(new Interceptor[]{pageInterceptor});

        return factoryBean;
    }

    /* 事务管理器 */
    @Bean
    public DataSourceTransactionManager transactionManager(){
        return new DataSourceTransactionManager(dataSource());
    }

}
